﻿namespace csharp基础;


public class 引用
{   
    //堆栈问题.局部变量声明的基本类型.会放到栈(复制值).
    //拆装箱,作为成员或数组元素时在堆中(公用地址)
    //string 类型在堆中.但是特殊(开辟新空间,复制)
    public static void  yinyongText()
    {
        int a = 10;
        int b = a;
        int[] array = { 1, 2, 3, 4 };
        int[] array1 = array;
        Console.WriteLine("array1[0]={0},b= {1}",array[0],a);

        b = 20;
        array1[0] = 5;
        Console.WriteLine("===========================");
       
        Console.WriteLine("array1[0]={0},b= {1}",array1[0],b);
        Console.WriteLine("array[0]={0},a= {1}",array[0],a);

         String s1= "123";
        //String s2= s1;
        String s2= "234";

        Console.WriteLine(s1);
        Console.WriteLine(s2);
        
    }

    public static int[] XuanZe(int[] array)
    {
        int temp;
        for (int i = 0; i < array.Length; i++)
        {   
            for (int j = i+1; j < array.Length; j++)
            {   
                //记录第一个元素的索引 查询后面的元素,最大的放到第一位
                temp = array[i];
                if (temp<array[j])
                {
                    array[i] = array[j];
                    array[j] = temp;
                }
            }
        }
        return array;
    }

    public static int[] MaoPao(int[] array)
    {
        for (int i = 0; i < array.Length-1; i++) {
            bool swapped = false;
            for (int j = 0; j < array.Length-i-1; j++) {
                if (array[j] >array[j+1]) { // 相邻元素比较 
                    // 可能多次交换 (拆构交换6.0新特性)
                    (array[j], array[j+1]) = (array[j+1], array[j]);
                    swapped = true;
                    Console.WriteLine(" swapped = true");
                }
            }

            if (!swapped) break;
        }
        return array;
    }

    static void Main()
    {

        int x = 5;
        //yinyongText();
        int[] array = new int[10]{1,2,3,4,5,6,7,8,9,0};
        Console.Write("排序前:");
        for (int i = 0; i < array.Length; i++)
        {
            
            Console.Write(array[i]);
            if ( i < array.Length-1)
            {
                Console.Write(", ");
            }
        }
        /*Console.WriteLine();
        Console.WriteLine("选择排序后:");
        for (int i = 0; i < array.Length; i++)
        {
            Console.Write(XuanZe(array)[i]);
            if ( i < array.Length-1)
            {
                Console.Write(", ");
            }
        }*/
        Console.WriteLine();
        Console.WriteLine("冒泡排序后:");
        for (int i = 0; i < array.Length; i++)
        {
            Console.Write(MaoPao(array)[i]);
            if ( i < array.Length-1)
            {
                Console.Write(", ");
            }
        }
    }
}